﻿using SpeechLib;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using Zzj.Pages;
using Zzj.Utils;
using Zzj.Windows;

namespace Zzj
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        private DispatcherTimer timer;
        private String TextProperty1 = String.Empty;
        private SpVoice speech = new SpVoice();
        private MainWindow mainWindow;

        public MainWindow()
        {
            InitializeComponent();
            DataContext = this;

            timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromSeconds(1);
            timer.Tick += new EventHandler(timeCycle);
            timer.Start();

            mainWindow = this;

            this.Loaded += Window_Load;
            this.Unloaded += Window_UnLoad;
        }

        private void Window_Load(object sender, EventArgs e)
        {
            string sMsg = string.Empty;
            if ("1".Equals(App.getConfig().IDCardDevice))//是否打开设备
            {
                string CardReaderDeviceType = App.getConfig().CardReaderDeviceType;//获取使用的读卡器品牌
                if (CardReaderDeviceType == "1")//神思读卡器
                {
                    string port = App.getConfig().SSIDCardPort;
                    string rate = App.getConfig().SSIDCardPortBaudRate;
                    int handle = CommonInterface.OpenDevice(port, rate, "");
                    if (handle <= 0)
                    {
                        MessageBox.Show("打开设备失败，错误代码：" + handle.ToString());
                    }
                }
                else if (CardReaderDeviceType == "2")//精伦读卡器
                {
                    int code = JLIDcardread.Opendevice();//打开读卡设备
                    if (code != 1)
                    {
                        MessageBox.Show("打开设备失败");
                    }
                }
            }

            Index indexPage = new Index(this);

            frmMain.Content = indexPage;
        }

        private void Window_UnLoad(object sender, EventArgs e)
        {
            string CardReaderDeviceType = App.getConfig().CardReaderDeviceType;//获取使用的读卡器品牌
            string sMsg = string.Empty;
            if (CardReaderDeviceType == "1")//神思读卡器
            {
                string port = App.getConfig().SSIDCardPort;
                string rate = App.getConfig().SSIDCardPortBaudRate;
                int handle = CommonInterface.OpenDevice(port, rate, "");
                if (handle <= 0)
                {
                    MessageBox.Show("打开设备失败，错误代码：" + handle.ToString());
                    return;
                }
            }
            else if (CardReaderDeviceType == "2")//精伦读卡器
            {
                try
                {
                    JLIDcardread.closeID();//关闭设备
                }
                catch { }
            }
        }

        public void Speak(String message)
        {
            speech.Speak(message, SpeechVoiceSpeakFlags.SVSFlagsAsync);
        }

        public void BreakSpeak()
        {
            speech.Speak(string.Empty, SpeechVoiceSpeakFlags.SVSFPurgeBeforeSpeak);
        }

        public void timeCycle(object sender, EventArgs e)
        {
            new Thread(() =>
            {
                Application.Current.Dispatcher.BeginInvoke(
                    new Action(() =>
                    {
                        TextProperty1 = DateTime.Now.ToString();
                        this.labelTime.Content = TextProperty1;
                    }));
            }).Start();
        }

        private void imgLogo_MouseUp(object sender, MouseButtonEventArgs e)
        {
            PasswordWindow passwordWindow = new PasswordWindow();
            passwordWindow.Owner = mainWindow;
            bool result = (bool)passwordWindow.ShowDialog();
            if (!result)
            {
                return;
            }

            SettingWindow win = new SettingWindow();
            win.Owner = mainWindow;
            result = (bool)win.ShowDialog();
            if (result)
            {
                Application.Current.Shutdown();
            }
        }
    }
}